package com.testDemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.alibaba.druid.util.StringUtils;

public class PayUtil {
	
	
	
	/**
	 * 组装请求数据字符串
	 * 
	 * @author hj
	 * @param "xml" 根元素名称
	 * @param postData
	 *            请求数据
	 * @return
	 */
	public static String toXml(Map<String, String> postData)
	{
		Element root = DocumentHelper.createElement("xml");
		Document document = DocumentHelper.createDocument(root);

		List<String> keys = new ArrayList<String>(postData.keySet());
		Collections.sort(keys);
		for (int i = 0; i < keys.size(); i++)
		{
			String key = keys.get(i);
			String value = postData.get(key);
			Element element = root.addElement(key);
			element.addText(StringUtils.isEmpty(value) ? "" : value);
		}

		/*Iterator<String> it = postData.keySet().iterator();
		while (it.hasNext())
		{
			String key = it.next();
			String value = postData.get(key);
			Element element = root.addElement(key);
			element.addCDATA(StringUtils.isEmpty(value) ? "" : value);
		}*/
		return document.asXML();
	}
public static void main(String[] args) {
	Map<String, String> resultMap = new HashMap<String, String>();
	resultMap.put("return_code", "SUCCESS");
	resultMap.put("return_msg", "OK");
	System.out.println(PayUtil.toXml(resultMap));
}
}
